//
//  LCWenHTMLViewController+LCPingLun.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/11.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCWenHTMLViewController+LCPingLun.h"
#import "MCFireworksButton.h"
#import "UIViewController+LCLoadData.h"

@implementation LCWenHTMLViewController (LCPingLun)

#pragma mark - //1有分享  其他没分享
- (void)initWithAppraise:(UIView *)view share:(NSInteger)share
{
    
    UIView * lowView = [[UIView alloc]init];
    lowView.backgroundColor = [UIColor whiteColor];
    [view addSubview:lowView];
    [lowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        }else{
            make.bottom.equalTo(self.view.mas_bottom).offset(0);
        }
        make.height.mas_equalTo(50);
    }];

    UIView * viewBackColor = [[UIView alloc]init];
    [viewBackColor.layer setMasksToBounds:YES];
    [viewBackColor.layer setCornerRadius:16.5];
    viewBackColor.backgroundColor = RGBColor(243, 243, 243);
    [lowView addSubview:viewBackColor];
    
    UIImageView *imageViewBi = [[UIImageView alloc]init];
    self.imageViewBi = imageViewBi;
    imageViewBi.image = [UIImage imageNamed:@"编辑"];
    [viewBackColor addSubview:imageViewBi];
    
    [imageViewBi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewBackColor.mas_left).offset(15);
        make.width.mas_equalTo(15);
        make.bottom.equalTo(viewBackColor.mas_bottom).offset(-6.5);
        make.top.equalTo(viewBackColor.mas_top).offset(8.5);
    }];
    
    UITextView *textView = [[UITextView alloc] init];
    //    textView. = @"点击此处说两句...";
    //    textView.scrollEnabled = NO;
    textView.scrollsToTop = NO;
    textView.font = [UIFont systemFontOfSize:14];
    textView.backgroundColor = RGBColor(243, 243, 243);
    //当textview的字符串为0时发送（rerurn）键无效
    textView.enablesReturnKeyAutomatically = YES;
    textView.keyboardType = UIKeyboardTypeDefault;
    //    //键盘return样式变成发送
    textView.returnKeyType = UIReturnKeySend;
    [viewBackColor addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewBackColor.mas_left).offset(35);
        make.right.equalTo(viewBackColor.mas_right).offset(-10);
        make.bottom.equalTo(viewBackColor.mas_bottom).offset(0);
        make.top.equalTo(viewBackColor.mas_top).offset(0);
        //        make.height.mas_equalTo(@15);
    }];
    UILabel * uilabel = [[UILabel alloc]init];
    uilabel.text = @"请填写详细地址";
    uilabel.enabled = NO;//lable必须设置为不可用
    uilabel.font = [UIFont systemFontOfSize:13];
    uilabel.backgroundColor = [UIColor clearColor];
    [textView addSubview:uilabel];
    //WithFrame:CGRectMake(2,3, textView.frame.size.width, 20
    [uilabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textView.mas_left).offset(3);
        make.right.equalTo(textView.mas_right).offset(-0);
        make.bottom.equalTo(textView.mas_bottom).offset(-10);
        make.top.equalTo(textView.mas_top).offset(10);
        //        make.height.mas_equalTo(@15);
    }];
    
    UIButton * shareBut = [[UIButton alloc]init];
    [lowView addSubview:shareBut];
    if (share == 1) {
        shareBut.hidden = NO;
    }else{
        shareBut.hidden = YES;
    }
    [lowView addSubview:shareBut];

    [shareBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lowView.mas_right).offset(-10);
        make.bottom.equalTo(lowView.mas_bottom).offset(0);
        make.top.equalTo(lowView.mas_top).offset(0);
        make.width.mas_equalTo(50);
    }];
    
    UIImageView * imageShare = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 20.5, 20.5)];
    imageShare.image = [UIImage imageNamed:@"分享"];
    [shareBut addSubview:imageShare];

    [imageShare mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(shareBut.mas_left).offset(15);
        make.bottom.equalTo(shareBut.mas_bottom).offset(-14.5);
        make.top.equalTo(shareBut.mas_top).offset(13);
        make.width.mas_equalTo(@20.5);
    }];
    
    ///点赞
    MCFireworksButton *likeButton = [[MCFireworksButton alloc] init];
    self.likeButton = likeButton;
    self.likeButton.particleImage = [UIImage imageNamed:@"Sparkle"];
    self.likeButton.particleScale = 0.05;
    self.likeButton.particleScaleRange = 0.02;
    [self.likeButton setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
    [self.likeButton addTarget:self action:@selector(handleButtonPress:) forControlEvents:(UIControlEventTouchUpInside)];
    [lowView addSubview:self.likeButton];

    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(shareBut.mas_left).offset(-11);
        make.bottom.equalTo(shareBut.mas_bottom).offset(-15.5);
        make.top.equalTo(shareBut.mas_top).offset(10);
        make.width.mas_equalTo(@22.5);
    }];
    
    ///评论数
    UIImageView *imagePingLunNum = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 20.5, 20.5)];
    self.imagePingLunNum = imagePingLunNum;
    imagePingLunNum.image = [UIImage imageNamed:@"留言"];
    [lowView addSubview:imagePingLunNum];

    [imagePingLunNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likeButton.mas_left).offset(-30);
        make.bottom.equalTo(self.likeButton.mas_bottom);
        make.top.equalTo(shareBut.mas_top).offset(13);
        make.width.mas_equalTo(@20.5);
    }];
    UIView *imagePingLunNumBg = [[UIView alloc] init];
    [lowView addSubview:imagePingLunNumBg];
//    imagePingLunNumBg.backgroundColor = arc4random_color;//调用随机色
//    imagePingLunNumBg.alpha = 0.6;
    [imagePingLunNumBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(imagePingLunNum).offset(-10);
        make.right.bottom.equalTo(imagePingLunNum).offset(10);
    }];
    
    UIView *imageShareBg = [[UIView alloc] init];
    [lowView addSubview:imageShareBg];
//    imageShareBg.backgroundColor = arc4random_color;//调用随机色
//    imageShareBg.alpha = 0.6;
    [imageShareBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(imageShare).offset(-10);
        make.right.bottom.equalTo(imageShare).offset(10);
    }];

    self.pingLunNumLabel = [[UILabel alloc] init];
    self.pingLunNumLabel.text = @"0";
    self.pingLunNumLabel.textColor = [UIColor whiteColor];
    self.pingLunNumLabel.font = [UIFont systemFontOfSize:11];
    self.pingLunNumLabel.backgroundColor = [UIColor colorWithRed:0.94 green:0.33 blue:0.34 alpha:1.00];
    self.pingLunNumLabel.textAlignment = NSTextAlignmentCenter;
    self.pingLunNumLabel.layer.masksToBounds = YES;
    // 设置圆角大小
    self.pingLunNumLabel.layer.cornerRadius = 6.0 ;
    
    [imagePingLunNum addSubview:self.pingLunNumLabel];
    [self.pingLunNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imagePingLunNum.mas_top).offset(-5);
        make.width.greaterThanOrEqualTo(@(20));
        make.height.mas_equalTo(12);
        make.centerX.equalTo(imagePingLunNum.mas_right);
    }];
    
    [viewBackColor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lowView.mas_left).offset(15);
        make.right.equalTo(imagePingLunNum.mas_left).offset(-30);
        make.bottom.equalTo(lowView.mas_bottom).offset(-8);
        make.top.equalTo(lowView.mas_top).offset(7.5);
    }];
    imagePingLunNum.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesturRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushPingLunVC)];
    [imagePingLunNum addGestureRecognizer:tapGesturRecognizer];
    __weak typeof(self) weakSelf = self;
    [imagePingLunNumBg tapGesture:^(UIGestureRecognizer *ges) {
        [weakSelf pushPingLunVC];
    }];
    [imageShareBg tapGesture:^(UIGestureRecognizer *ges) {
        [weakSelf shareBtnAction];
    }];
//    block(lowView,textView,uilabel,shareBut,imageShare,self.likeButton,imagePingLunNum);
    
    textView.delegate = self;
    weakSelf.imageShare = imageShare;
    weakSelf.lowView = lowView;
    weakSelf.textView = textView;
    uilabel.text = @"写评论....";
    self.uilabel = uilabel;
    self.webView.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}

-(void)pushPingLunVC{
    NSString *resource_id,*type;
    if (self.model) {
        resource_id = self.model.id;
        type = self.model.type;
    }

    LCHomePingLunTableViewController *homePingLunTableViewController = [[LCHomePingLunTableViewController alloc] init];
    homePingLunTableViewController.id_id = resource_id;
    homePingLunTableViewController.type = type;
    [self.navigationController pushViewController:homePingLunTableViewController animated:YES];
}
- (IBAction)handleButtonPress:(id)sender {
    NSString *resource_id,*type;
    if (self.model) {
        resource_id = self.model.id;
        if (resource_id == nil && self.isPushType) {
            @try {
                NSArray *urlArr = [self.webView.URL.absoluteString componentsSeparatedByString:@"/"];
                resource_id = urlArr[urlArr.count-2];
            } @catch (NSException *exception) { }
        }
        type = self.model.type;
    }

    [self.likeButton popOutsideWithDuration:0.5];
    [self.likeButton setImage:[UIImage imageNamed:@"点赞_sel"] forState:UIControlStateNormal];
    [self.likeButton animate];
    if (self.likeButtonSelected == YES) {//已点赞
        return;
    }
    [LSZRequestData UserVersusResourceThumbUp:resource_id type:type :^(BOOL isOk, NSString * str, NSDictionary * dic) {
        if (isOk == YES) {
        }
        else{ //            [self setStringWarning:str];
        }
    }];
}

#pragma mark -  评论  -
- (void)keyboardChangedWithTransition:(YYKeyboardTransition)transition {
    CGRect frame = self.manager.keyboardFrame;
    frame = [self.manager convertRect:frame toView:self.view];
    
    // 获取键盘当前状态
    BOOL visible = self.manager.keyboardVisible;
    
    if (visible) {
    }else{
        self.checkLoginSum = 0;///监听键盘缩回去的时候 初始化校验
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.4];
            [self.lowView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self.view);
                if (@available(iOS 11, *)) {
                    make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
                }else{
                    make.bottom.equalTo(self.view.mas_bottom).offset(0);
                }
                make.height.mas_equalTo(self.lowView.frame.size.height);
            }];
        [UIView commitAnimations];
    }
}
- (void)addkeyboardWillShow
{
    //监听键盘
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification {
    if([[_UserM usercode] isEqualToString:@""]){
        self.checkLoginSum++;
        if (self.checkLoginSum > 1) return;
        pushAccLoginVC;
        [self.view endEditing:YES];
        return;
    }
    
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat height = keyboardRect.size.height;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    [self.lowView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-height);
        make.height.mas_equalTo(50);
    }];
    [UIView commitAnimations];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString * strLong = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (strLong.length == 0) {
        self.uilabel.hidden = NO;
    }else{
        if (strLong.length > 0 &&self.uilabel.hidden == NO) {
            self.uilabel.hidden = YES;
        }
    }
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        NSLog(@"点击发送键");
        [self initWithUserCommentsOnResources:strLong];
        [self.view endEditing:YES];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    return YES;
}
#pragma mark - 进行评价
-(void)initWithUserCommentsOnResources :(NSString *)content {
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    NSString *resource_id,*type;
    if (self.model) {
        resource_id = self.model.id;
        type = self.model.type;
    }
    //    NSLog(@"resource_id = %@", resource_id);
    //    NSLog(@"type = %@", type);
    ///监听键盘事件,回车进行发送评论
    __weak typeof(self) weakSelf = self;
    [LSZRequestData UserCommentsOnResources:resource_id content:content type:type :^(BOOL isOK, NSString * Str, NSDictionary * dic) {
        if (isOK == YES) {
            weakSelf.textView.text = @"";
//            [weakSelf addLCBeautifulEssayControllerLingCommentsIncomesDataWithTid:resource_id];
            [SVProgressHUD showSuccessWithStatus:@"评论成功!"];
            [SVProgressHUD dismissWithDelay:1];

        }else{
            [SVProgressHUD showErrorWithStatus:Str];
            [SVProgressHUD dismissWithDelay:1];
        }
    }];
}

@end
